﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.General;
using ESF.General.Entity;
using ESF.SFT.FileUploader.Presenter.FileReceiverGeneral;
using ESF.General.Text;

#endregion


namespace ESF.SFT.FileUploader.Presenter.Users
{
	public class SignInWindowPresenter : BasePresenter<ISignInWindowViewer>
	{
		#region Miembros

		private ReceiverGeneralClient _ReceiverGeneralClient;

		#endregion
		
		
		#region Constructor

		public SignInWindowPresenter (ISignInWindowViewer pViewer)
			: base (pViewer) {}

		#endregion


		#region Funciones

		public override void Load()
		{
			try
			{
				_ReceiverGeneralClient = new ReceiverGeneralClient ();
			}
			catch (Exception pEx)
			{
				LogError (pEx);
				return;
			}
			
			_Viewer.ProductVersion = ReadConfig.ExecutingVersion;
		}


		public void SignIn ()
		{
			List<Couple<string,string>> enterpriseData;
			
			List<FileRegisterValidatorSet> regValSets;


			Log.ClearSession ();

			_Viewer.ShowClearProgressDialog ();
			_Viewer.CurrentStepName = "Validando datos";
			
			if (TextHelper.IsEmpty(_Viewer.UserName))
			{
				_Viewer.FocusUserName ();
				throw new GenericInfoException ("Diligencie el Usuario");
			}

			if (TextHelper.IsEmpty(_Viewer.UserPassword))
			{
				_Viewer.FocusUserPassword ();
				throw new GenericInfoException ("Diligencie la Contraseña");
			}

			
			Log.Session_UserName = _Viewer.UserName;

			_Viewer.CurrentAdvance100 = 30;
			_Viewer.CurrentStepName = "Iniciando sesión";

			enterpriseData = _ReceiverGeneralClient.StartSession (_Viewer.UserName, _Viewer.UserPassword);
			
			Log.Session_EnterpriseName = enterpriseData.Find (ed => ed.Key == "EnterpriseName").Value;
			Log.Session_EnterpriseIdentificationType = enterpriseData.Find (ed => ed.Key == "EnterpriseIdentificationType").Value;
			Log.Session_EnterpriseIdentificationNumber = enterpriseData.Find (ed => ed.Key == "EnterpriseIdentificationNumber").Value;
			Log.Session_EnterpriseEmail = enterpriseData.Find(ed => ed.Key == "EnterpriseEmail").Value;
			
			Log.Session_UserFullName = enterpriseData.Find (ed => ed.Key == "UserFullName").Value;
			Log.Session_UserEmail = enterpriseData.Find(ed => ed.Key == "UserEmail").Value;
			Log.Session_Ticket = enterpriseData.Find (ed => ed.Key == "Ticket").Value;

			_Viewer.CurrentAdvance100 = 60;
			_Viewer.CurrentStepName = "Obtener Validadores de los Anexos Tecnicos";

			
			regValSets = _Viewer.FileRegisterValidatorSets = _ReceiverGeneralClient.GetFileRegisterValidatorSets(Log.Session_Ticket, Log.Session_UserName);
			
			_Viewer.CurrentAdvance100 = 85;
			_Viewer.CurrentStepName = "Estableciendo Sesión";

			#region Session Info

			Log.Session_Info = string.Format("Usuario Nombre Completo: {0}\nUsuario Email: {1}\n\nEntidad: {2}\nEntidad Tipo ID: {3}\nEntidad Nro ID: {4}\nEntidad Correo Electrónico: {5}", 
				Log.Session_UserFullName,
				Log.Session_UserEmail,
				Log.Session_EnterpriseName,
				Log.Session_EnterpriseIdentificationType,
				Log.Session_EnterpriseIdentificationNumber,
				Log.Session_EnterpriseEmail);

			Log.Session_Info += string.Format("\n\nAnexos Técnicos vinculados ({0}):\n", regValSets.Count);

			foreach (FileRegisterValidatorSet frvs in regValSets)
			{
				Log.Session_Info += 
					string.Format(" - {0}\n -- Firma Digital:{1}{2}\n", frvs.Name,
					(frvs.RequiresDigitalCertificate ? " Requerido" : " No Requerido"),
					(frvs.OptionalDigitalCertificate ? " y Opcional" : string.Empty));

				if (frvs.RequiresCMS)
				{
					Log.Session_Info += string.Format(" -- Sobre Digital requerido\n");
				}
			}

			_Viewer.CurrentAdvance100 = 95;
			
			#endregion

			_Viewer.CurrentStepName = "Cargando ventana de Archivos";
			_Viewer.InitializeWindows ();
			_Viewer.CurrentAdvance100 = 100;

			Log.StopOps ();
		}

		#endregion
	}
}
